<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!--<link rel="stylesheet" href="">-->
    <style type="text/css">
        .list_con {
            width: 600px;
            margin: 50px auto 0;
        }

        .inputtxt {
            width: 550px;
            height: 30px;
            border: 1px solid #ccc;
            padding: 0px;
            text-indent: 10px;
        }

        .inputbtn {
            width: 40px;
            height: 32px;
            padding: 0px;
            border: 1px solid #ccc;
            cursor: pointer;
        }

        .list {
            margin: 0;
            padding: 0;
            list-style: none;
            margin-top: 20px;
        }

        .list li {
            height: 40px;
            line-height: 40px;
            border-bottom: 1px solid #ccc;
        }

        .list li span {
            float: left;
        }

        .list li a {
            float: right;
            text-decoration: none;
            /*margin: 0 10px;*/
        }
        a{
            width: 40px;
            text-align: center;
        }
    </style>
    <script type="text/javascript" src="vue/vue.min.js"></script>

</head>
<body>
<div class="list_con">
    <h2>To do list</h2>
    <input type="text" name="" id="txt1" class="inputtxt" v-model="content">
    <input type="button" name="" value="增加" id="btn1" class="inputbtn"
           @click="append">

    <ul id="list" class="list">

        <li v-for="(i, j) in list_sub"><span>{{j+1}}、{{i}}</span>
            <a href="javascript:;" class="up" @click="up(j)"> ↑ </a>
            <a href="javascript:;" class="down" @click="down(j)"> ↓ </a>
            <a href="javascript:;" class="del" @click="del(j)">删除</a>
        </li>
        <!--<li><span>学习css</span><a href="javascript:;" class="up"> ↑ </a><a-->
                <!--href="javascript:;" class="down"> ↓ </a><a href="javascript:;"-->
                                                           <!--class="del">删除</a>-->
        <!--</li>-->
        <!--<li><span>学习javascript</span><a href="javascript:;" class="up"> ↑ </a><a-->
                <!--href="javascript:;" class="down"> ↓ </a><a href="javascript:;"-->
                                                           <!--class="del">删除</a>-->
        <!--</li>-->

    </ul>

</div>
</body>
<script>
    var vm = new Vue({
        el:".list_con",
        data:{
            content:"",
            list_sub:["学习html", "学习css", "学习javascript"]
        },
        methods:{
            append:function () {
                // splice(a,b,c) a开始的位置，b删除的个数，c添加的内容
                if (vm.content == ""){
                    return
                }
                vm.list_sub.splice(0, 0, vm.content);
                vm.content = ""
            },
            up:function (index) {
                if (index == 0){alert("已经是第一个"); return}
                // 选中此行删除，在粘贴到上一行
                var data = vm.list_sub[index];
                console.log(vm.list_sub[index]);
                console.log(index);
                vm.list_sub.splice(index, 1);  // 删除下标为index、长度为1的数据
                vm.list_sub.splice(index-1, 0, data) // 在下标为index-1的位置添加数据data
            },
            down:function (index) {
                if (vm.list_sub.length-1 == index){
                    alert("已经最后一个");
                    return
                }
                var data = vm.list_sub[index];
                vm.list_sub.splice(index, 1);
                vm.list_sub.splice(index+1, 0, data)

            },
            del:function (index){
                // console.log(1);
                vm.list_sub.splice(index, 1)
            },
        },
    })
</script>
</html>